﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CongThongTin.Entities;

namespace CongThongTin
{
    public partial class ChiTiet : System.Web.UI.Page
    {
        CongThongTinEntities _entities = new CongThongTinEntities();
        static int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Convert.ToInt32(Request.Params["id"].ToString());
            int tl = Convert.ToInt32(Request.Params["tl"].ToString());
            if (!IsPostBack)
                loadChiTietTinTuc(id);
            loadTinTucLienQuan(tl);
        }

        void loadChiTietTinTuc(int maTinTuc)
        {
            Entities.TINTUC oTinTuc = _entities.TINTUCs.SingleOrDefault(t => t.MATINTUC == maTinTuc);

            StringBuilder tinTucHTML = new StringBuilder();
            tinTucHTML.Append("<ol id='singlecomments' class='commentlist'>");
			tinTucHTML.Append("<li class= 'clearfix'>");
            //tinTucHTML.Append(" <div class='message'>");
            tinTucHTML.Append("<h3>"+oTinTuc.TIEUDE+"</h3><p>"+oTinTuc.NOIDUNG+"</p>");
            tinTucHTML.Append("<div class='clear'></div> </li>  </ol>");

            tbChiTiet.Text = tinTucHTML.ToString();
        }

        void loadTinTucLienQuan(int maLoaiTin)
        { 
            List<TINTUC> lTintuc=   (   from t in _entities.TINTUCs
                                        where t.MALOAITINTUC==maLoaiTin
                                        select t)
                                   .OrderBy(t=>t.NGAYDANGTIN)
                                   .Take(5)
                                   .ToList();

            if (lTintuc.Count > 0)
            {
                string script = @"  $(function () { $('#divTinTucLienQuan').show(); });";
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString() + "RegisterLoadDisplay", script, true);
            }


            StringBuilder tinTucHTML=new StringBuilder ();
            tinTucHTML.Append("<h3>Các tin khác</h3><br/><table>");

            foreach (TINTUC t in lTintuc)
            {
                tinTucHTML.Append("<tr>");
                tinTucHTML.Append("<td>").Append("<a href=\"ChiTiet.aspx?id="+t.MATINTUC+"&tl="+t.MALOAITINTUC+"\">"+t.TIEUDE+"</a>");
                tinTucHTML.Append("</td>");
                tinTucHTML.Append("</tr>");
            }
            
            tinTucHTML.Append("</table>");

            ltrTienLienQuan.Text = tinTucHTML.ToString();
        }
    }
}